package houseHold;

import houseHold.electricObjects.ElectricObjectType;
import sensor.Sensor;
import RoomPanel.IDGenerator;
import constants.Constants;
import consumption.ConsumptionType;

public class HouseHoldObject {
	private boolean active;
	private int activeDuration;
	protected int consumption;
	private int consumptionCoefficient;
	protected ConsumptionType cType;
	protected Room location;
	protected String name;
	protected int objectID;
	protected int pastActivity;
	protected Sensor sensor;
	protected ObjectType type;

	public HouseHoldObject(boolean newIDRequest) {
		setActive(false);
		setConsumptionCoefficient(100);
		setConsumption(0);
		type = GHObject.OBJECT;
		cType = ConsumptionType.CONSUMPTION;
		if (newIDRequest) {
			objectID = IDGenerator.getNextObjectID();
			IDGenerator.incrementObjectID();
		}
		name = type.toString() + "#" + objectID;
		setLocation(null);
		setSensor(null);
	}

	public void addPastActivity(int time) {
		pastActivity += time;

	}

	public void decrementActiveDuration(int val) {
		activeDuration -= val;
		if (activeDuration <= 0) {
			activeDuration = 0;
			if (this.type != ElectricObjectType.REFRIGERATOR)
				this.active = false;
			else {
				this.consumptionCoefficient = 75;
			}
		}
	}

	public int getActiveDuration() {
		return activeDuration;
	}

	public int getConsumption() {
		return consumption;
	}

	public int getConsumptionCoefficient() {
		return consumptionCoefficient;
	}

	public ConsumptionType getConsumptionType() {
		return cType;
	}

	public double getCurrentConsumption() {
		int variable = consumption / 15;
		double davg = 1.0 / ((double) Constants.rd.nextInt(100) + 1.0);
		int avg = 0;
		if (variable > 0)
			avg = Constants.rd.nextInt(variable);
		boolean sw = Constants.rd.nextBoolean();
		davg += avg;
		if (sw) {
			davg = davg * (-1);
		}
		return (consumption + davg) * consumptionCoefficient / 100;
	}

	public int getID() {
		return objectID;
	}

	public Room getLocation() {
		return location;
	}

	public String getName() {
		return name;
	}

	public int getPastActivity() {
		return pastActivity;
	}

	public Sensor getSensor() {
		return sensor;
	}

	public ObjectType getType() {
		return type;
	}

	public boolean isActive() {
		return active;
	}

	public HouseHoldObject modifyConsumptionType(ConsumptionType cType) {
		return HouseHoldObjectFactory.getObject(cType, this);
	}

	public void moveItem(Room loc) {
		if (loc != null) {
			location.removeItem(this.name);
			loc.addItem(this);
		} else {
			this.location = loc;
		}
	}

	public String print() {
		String object = "\nObject:";
		object = object.concat("\nName = " + name);
		if (type != null)
			object = object.concat("\nType = " + type.toString());
		else
			object = object.concat("\nType = n\\a");
		object = object.concat("\nConsumption = " + consumption);
		object = object.concat("\nConsumption Type = " + cType.toString());
		object = object.concat("\nConsumption coefficient = "
				+ consumptionCoefficient);
		object = object.concat("\nActive = " + isActive());
		if (location != null)
			object = object.concat("\nLocation = " + location.getName());
		else
			object = object.concat("\nLocation = n\\a");
		if (sensor != null)
			object = object.concat("\nSensor = " + sensor.toString());
		else
			object = object.concat("\nSensor = n\\a");
		return object;
	}

	public void setActive(boolean active) {
		this.active = active;
		pastActivity = 0;
	}

	public void setActiveDuration(int activeDuration) {
		if (this.type != ElectricObjectType.REFRIGERATOR) {
			this.activeDuration = activeDuration;
			pastActivity = 0;
		} else {
			this.activeDuration = activeDuration;
			this.activeDuration *= 10;
			pastActivity = 0;
			this.consumptionCoefficient = 250;
		}

	}

	public void setConsumption(int consumption) {
		this.consumption = consumption;
	}

	public void setConsumptionCoefficient(int consumptionCoefficient) {
		this.consumptionCoefficient = consumptionCoefficient;
	}

	public void setID(int id) {
		this.objectID = id;
	}

	public void setLocation(Room loc) {
		this.location = loc;
		if (loc != null) {
			location.addItem(this);
		}
	}

	public void setLocation_r(Room loc) {
		this.location = loc;
	}

	public void setName(String name) {
		String old = this.name;
		this.name = name;
		this.location.overwrite(old, name, this);
	}

	public void setName_NoOverWrite(String name) {
		this.name = name;
	}

	public void setSensor(Sensor sensor) {
		this.sensor = sensor;
	}

	public void setType(ObjectType type, boolean overwrite) {
		this.type = type;
		if (overwrite)
			this.setName(type.toString() + "#" + objectID);
		else
			this.setName_NoOverWrite(type.toString() + "#" + objectID);
	}

	@Override
	public String toString() {
		return name;
	}

}
